package rjeutsky.database.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import rjeutsky.database.interfaces.CRUDDomainRepository;
import rjeutsky.models.UserRole;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

public class UserRoleDomainRepository implements CRUDDomainRepository<UserRole> {

	private static final String CREATE_SQL_NO_ID = "INSERT INTO UserRole (Name) VALUES (?)";
	private static final String CREATE_SQL = "INSERT INTO UserRole (Id, Name) VALUES (?, ?)";
	
	private PreparedStatement createStatement;
	private PreparedStatement createStatementNoId;
	
	public UserRoleDomainRepository(Connection connection) throws SQLException {
		createStatement = connection.prepareStatement(CREATE_SQL, PreparedStatement.RETURN_GENERATED_KEYS);
		createStatementNoId = connection.prepareStatement(CREATE_SQL_NO_ID, PreparedStatement.RETURN_GENERATED_KEYS);
	}
	
	@Override
	public void create(UserRole item) throws SQLException {
		if (item.getId() == null) {
			createStatementNoId.setString(1, item.getName());
			createStatementNoId.executeUpdate();
			ResultSet generatedKeys = createStatementNoId.getGeneratedKeys();
			generatedKeys.next();
			item.setId(generatedKeys.getLong(1));
		} 
		else {
			createStatement.setLong(1, item.getId());
			createStatement.setString(2, item.getName());
			createStatement.executeUpdate();
		}
	}

	@Override
	public void delete(long id) {
		// TODO Auto-generated method stub
		throw new NotImplementedException();
	}

	@Override
	public List<UserRole> query() {
		// TODO Auto-generated method stub
		throw new NotImplementedException();
	}

	@Override
	public UserRole read(long id) {
		// TODO Auto-generated method stub
		throw new NotImplementedException();
	}

	@Override
	public void update(UserRole item) {
		// TODO Auto-generated method stub
		throw new NotImplementedException();
	}
	
}
